<?php

namespace app\controller;

use app\model\CaptchaModel;
use app\model\CodeModel;
use app\model\UserModel;
use Gregwar\Captcha\CaptchaBuilder;

class Basic extends Base
{
    //
    /**
     * @api 获取图形验证码
     * @method GET
     * @return \think\response\File
     */
    public function captcha()
    {
        $captcha = new CaptchaBuilder();
        $captcha->build();
        $model = CaptchaModel::create(['captcha' => $captcha->getPhrase()]);
        $header = ['Captcha-Id' => $model['id']];
        return download($captcha->get(), 'captcha.jpg', true)->mimeType('image/jpeg')->header($header)->force(false);
    }

    //

    /**
     * 发送短信验证码
     * @api
     * @method POST
     * @param string $phone 手机号
     * @param numeric $captcha_id 验证码ID
     * @param string $captcha 验证码
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function send_code(string $phone,int $captcha_id,string $captcha)
    {
        $cap = CaptchaModel::where(['id' => $captcha_id, 'captcha' => $captcha])->find();
        if (empty($cap)) abort(403, '验证码错误');
        $cap->delete();
        $code = rand(100000, 999999);
        $model = CodeModel::create(['phone' => $phone, 'code' => $code]);
        $sms = '您正在申请注册【咪啪】，验证码是' . $code . '。';
        //todo 发短信
        return json(['message' => '验证码已发送', 'sms' => $sms]);
    }

    /**
     * 注册账号
     * @api
     * @method POST
     * @param string $phone 手机号
     * @param string $password 密码
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function register()
    {
        $data = $this->request->only(['phone', 'password', 'code']);
        $code = CodeModel::where(['phone' => $data['phone'], 'code' => $data['code']])->find();
        if (empty($code)) abort(403, '验证码错误');
        CodeModel::where('id', $code['id'])->delete();//删除验证码
        $data['nickname'] = '用户' . substr($data['phone'], 0, -4);
        $data['password'] = sha1($data['password'] . env('KEY'));
        $data['token'] = sha1(microtime() . rand(0, 9999));
        $data['reg_ip'] = $this->request->ip();
        $data['client'] = $this->request->header();
        UserModel::create($data);
        return json(['message' => '注册成功']);

    }

    /**
     * 用户登录
     * @param string $phone 手机号
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login()
    {
        $data = $this->request->only(['phone', 'email', 'password']);
        $user = UserModel::where('phone', $data['phone'])->find();
        if (empty($user)) abort(404, '该手机号未注册');
        if ($user['password'] != sha1($data['password'] . env('key'))) abort(403, '密码错误');
        return json(['message' => '登录成功', 'token' => $user['token']]);
    }
}
